﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class LayAirTerminalLineForm : System.Windows.Forms.Form
	{
		public LayAirTerminalLineForm()
		{
			this.InitializeComponent();
		}

		public LayAirTerminalLineForm(HVACUserSetting userSetting, string name)
		{
			this.InitializeComponent();
			this.initlbText(name);
			this.initXmlboot(name);
		}

		public LayAirTerminalLineForm(UserSetting userSetting, string name)
		{
			this.InitializeComponent();
			this.initlbText(name);
			this.initXmlboot(name);
		}

		private void rdoLimitNumber_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitNumber.Checked)
			{
				this.rdoLimitDistance.Checked = false;
				this.RdoNumberGroup(true);
				return;
			}
			this.RdoNumberGroup(false);
		}

		private void initlbText(string name)
		{
			this.Text = name + "沿线布置";
			this.groupBox4.Text = "首个" + name + "与起点的距离";
			this.label5.Text = "和" + name + "间距的比值：";
			this.groupBox5.Text = "第一个" + name + "与起点的距离";
		}

		private void initXmlboot(string name)
		{
			if (name == "风口")
			{
				this.xmlbootname = "ductlayline";
				return;
			}
			if (name == "风盘")
			{
				this.xmlbootname = "coillayline";
				return;
			}
			if (name == "器具")
			{
				this.xmlbootname = "PlumbingFixtureslayline";
			}
		}

		private void RdoNumberGroup(bool check)
		{
			this.hytbCount.Enabled = check;
			this.hytbRatio.Enabled = check;
		}

		private void RdoDistanceGroup(bool check)
		{
			this.hytbDistance.Enabled = check;
			this.hytbDistanceSide.Enabled = check;
		}

		private void rdoLimitDistance_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoLimitDistance.Checked)
			{
				this.rdoLimitNumber.Checked = false;
				this.RdoDistanceGroup(true);
				return;
			}
			this.RdoDistanceGroup(false);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			string limitStr = "1";
			if (this.rdoLimitNumber.Checked)
			{
				this.layLineParameter.LimitFlag = 1;
				this.layLineParameter.Count = Convert.ToInt32(this.hytbCount.Text.Trim());
				this.layLineParameter.Ratio = Convert.ToDouble(this.hytbRatio.Text.Trim());
				limitStr = "1";
			}
			else if (this.rdoLimitDistance.Checked)
			{
				this.layLineParameter.LimitFlag = 2;
				this.layLineParameter.Distance = Convert.ToDouble(this.hytbDistance.Text.Trim());
				this.layLineParameter.DistanceSide = Convert.ToDouble(this.hytbDistanceSide.Text.Trim());
				limitStr = "2";
			}
			this.LaySettingWrite(limitStr);
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void LaySettingWrite(string limitStr)
		{
			this.userSettingS.WriteValue(this.xmlbootname, "count", this.hytbCount.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "ratio", this.hytbRatio.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "distance", this.hytbDistance.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "sidedistance", this.hytbDistanceSide.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "limit", limitStr);
		}

		private void LayAirTerminalLineForm_Load(object sender, EventArgs e)
		{
			this.LaySettingInit();
		}

		private void LaySettingInit()
		{
			this.hytbCount.Text = this.userSettingS.GetValue(this.xmlbootname, "count");
			this.hytbRatio.Text = this.userSettingS.GetValue(this.xmlbootname, "ratio");
			this.hytbDistance.Text = this.userSettingS.GetValue(this.xmlbootname, "distance");
			this.hytbDistanceSide.Text = this.userSettingS.GetValue(this.xmlbootname, "sidedistance");
			if (this.userSettingS.GetValue(this.xmlbootname, "limit") == "2")
			{
				this.rdoLimitDistance.Checked = true;
				this.rdoLimitNumber.Checked = false;
				this.layLineParameter.LimitFlag = 2;
				this.layLineParameter.Distance = Convert.ToDouble(this.hytbDistance.Text.Trim());
				this.layLineParameter.DistanceSide = Convert.ToDouble(this.hytbDistanceSide.Text.Trim());
				return;
			}
			this.rdoLimitNumber.Checked = true;
			this.rdoLimitDistance.Checked = false;
			this.layLineParameter.LimitFlag = 1;
			this.layLineParameter.Count = Convert.ToInt32(this.hytbCount.Text.Trim());
			this.layLineParameter.Ratio = Convert.ToDouble(this.hytbRatio.Text.Trim());
		}

		private UserSetting userSettingS = new UserSetting();

		public LayLineParameter layLineParameter = new LayLineParameter();

		private string xmlbootname = "ductlayline";
	}
}
